package org.game.module.chatbo;

import java.util.List;

import org.bean.AccountInfo;
import org.bean.ChatRoom;
import org.bean.InterceptInfoDTO;
import org.bean.SourceChatMsg;
import org.cache.InterceptMappingCache;
import org.constant.ChatConstant;
import org.constant.InterceptConstant;
import org.error.GameErrorCode;
import org.game.constant.SessionConstent;
import org.game.db.bean.Player;
import org.game.db.proxy.bean.PlayerProxy;
import org.game.msgBuilder.ChatMsgBuilder;
import org.game.protobuf.c2s.C2SChatMsg.ChatMsg;
import org.game.protobuf.c2s.C2SChatMsg.ReqChangeRoom;
import org.game.protobuf.c2s.C2SChatMsg.ReqInitChatInfo;
import org.game.protobuf.s2c.S2CChatMsg;
import org.game.protobuf.s2c.S2CChatMsg.ChatInfo;
import org.game.protobuf.s2c.S2CChatMsg.RespChangeRoom;
import org.game.util.ProtoUnit;
import org.remote.social.RemoteSocialServer;
import org.utils.RemoteUtil;

import com.sojoys.artifact.exception.AbstractLogicModelException;
import com.sojoys.artifact.factory.session.ISession;
import com.sojoys.artifact.plugin.dirtyword.DirtyWordsPlugin;
import com.sojoys.artifact.tools.ToolError;

/**
 * @author : DengYing
 * @CreateDate : 2017年9月7日 下午6:53:05
 * @Description ：Please describe this document
 */
public class ChatBOImpl implements ChatBO {

	@Override
	public byte[] sendChatMsg(ISession session, ChatMsg msg) throws AbstractLogicModelException {
		Player player = session.getAttr(SessionConstent.PLAYER);
		PlayerProxy playerProxy = player.getPlayerProxy();
		String content = msg.getContent();
		
		boolean cmd = ChatCmdProxy.me().execute(player, content);

		if (!cmd) {
			//1.禁言
			InterceptInfoDTO dto = InterceptMappingCache.checkIntercept(InterceptConstant.INTERCEPT_TYPE_MUTE, String.valueOf(player.getId()));
			if (dto != null){
				return ProtoUnit.toByte(ChatMsgBuilder.me().buildChatMsg((int) (dto.getDeadLine().getTime()/1000)));
			}
			
			ToolError.isAndTrue(GameErrorCode.NOT_FOND_CHAT_ROOM,"还未进入聊天室",msg.getChannel() == ChatConstant.CHANNEL_WORLD && playerProxy.getChatRoomId() <= 0);
			
			// 聊天功能才检查屏蔽字
			if (msg.getFun() == ChatConstant.FUN_CHAT) {
				// 检查屏蔽字
				content = DirtyWordsPlugin.findAndReplace(content);
			}
			
			
			dto = InterceptMappingCache.checkIntercept(InterceptConstant.INTERCEPT_TYPE_MUTE_SILENT,String.valueOf(player.getId()));
			//2.静默禁言
			if (dto != null && msg.getChannel() == ChatConstant.CHANNEL_WORLD){
				SourceChatMsg sourceChatMsg = new SourceChatMsg(player.getId(), player.getName(),playerProxy.getChatRoomId(), msg.getChannel(),msg.getFun(), content, msg.getPlayerId());
				ChatInfo info = ChatMsgBuilder.me().builderChatInfo(sourceChatMsg);
				player.getPlayerProxy().sendMsg(ChatBO.pushChatMsg, info);
				return ProtoUnit.toByte(ChatMsgBuilder.me().buildChatMsg(0));
			}
			
			// 发送聊天
			SourceChatMsg sourceChatMsg = new SourceChatMsg(player.getId(), player.getName(),playerProxy.getChatRoomId(), msg.getChannel(),msg.getFun(), content,msg.getPlayerId());
			RemoteUtil.socialSync().sendChat(sourceChatMsg);	
		}
		return ProtoUnit.toByte(ChatMsgBuilder.me().buildChatMsg(0));
	}

	@Override
	public byte[] changeRoom(ISession session, ReqChangeRoom changeRoom) throws AbstractLogicModelException {
		
		ToolError.isAndTrue(GameErrorCode.CLIENT_PARAM_IS_ERR,"参数错误", changeRoom.getRoomId() > 100);
		
		Player player = session.getAttr(SessionConstent.PLAYER);
		PlayerProxy playerProxy = player.getPlayerProxy();
		
		ToolError.isAndTrue(GameErrorCode.NOT_FOND_CHAT_ROOM,"还未进入聊天室",playerProxy.getChatRoomId() <= 0);
		
		ChatRoom chatRoom = RemoteUtil.socialSync().changeRoomNumber(changeRoom.getRoomId(),player.getId());
		
		ToolError.isAndTrue(GameErrorCode.NOW_SOLE_CHAT_ROOM,"没有其他房间", chatRoom == null);
		playerProxy.setChatRoomId(chatRoom.getId());
		
		S2CChatMsg.RespInitChatInfo.Builder builder = S2CChatMsg.RespInitChatInfo.newBuilder();
		List<ChatInfo> infos = ChatMsgBuilder.me().builderChatInfo(chatRoom.getAllMsg());
		builder.addAllMsgs(infos);	
		builder.setRoomId(chatRoom.getId());
		RespChangeRoom.Builder resp = RespChangeRoom.newBuilder().setRoomInfo(builder);
		return ProtoUnit.toByte(resp.build());
	}

//	@Override
//	public byte[] getblacklist(ISession session, GetBlacklist req) throws AbstractLogicModelException {
//		Player player = session.getAttr(SessionConstent.PLAYER);
//		PlayerData playerData = player.getPlayerDatasFkPlayerId();
//		// 取得黑名单
//		List<Integer> blacklist = ToolMap.putIfAbsentList(PlayerDataSmallKey.TYPE_BLACKLIST, playerData.getData(), Lists.newArrayList());
//		
//		S2CChatMsg.Blacklist.Builder builder = S2CChatMsg.Blacklist.newBuilder();
//		builder.addAllPids(blacklist);
//		
//		return ProtoUnit.toByte(builder.build());
//	}
//
//	@Override
//	public byte[] blacklistOperate(ISession session, ReqBlacklistOperate req) throws AbstractLogicModelException {
//		Player player = session.getAttr(SessionConstent.PLAYER);
//		PlayerData playerData = player.getPlayerDatasFkPlayerId();
//		// 取得黑名单
//		List<Integer> blacklist = ToolMap.putIfAbsentList(PlayerDataSmallKey.TYPE_BLACKLIST, playerData.getData(), Lists.newArrayList());
//		OperateType type = req.getType();
//		
//		ChangeType changeType = null;
//		switch (type) {
//		case ADD:
//			blacklist.add(req.getTargetPid());
//			changeType = ChangeType.ADD;
//			break;
//		case REMOVED:
//			if (blacklist.contains(req.getTargetPid())) {
//				blacklist.remove((Integer)req.getTargetPid());
//				changeType = ChangeType.DEFAULT;
//			}
//			break;
//		}
//		if (changeType!=null) {
//			// 更新黑名单
//			S2CChatMsg.ChangeBlacklist.Builder builder = S2CChatMsg.ChangeBlacklist.newBuilder();
//			builder.setCt(changeType);
//			builder.setPid(req.getTargetPid());
//			
//			IMessage msg = MessageManager.me().create(ChatBO.changeBlacklistOperate, ProtoUnit.toByte(builder.build()));
//			player.getPlayerProxy().sendMsg(msg);
//		}
//		
//		return new byte[0];
//	}

	@Override
	public byte[] getInitChatInfo(ISession session, ReqInitChatInfo req) throws AbstractLogicModelException {
		Player player = session.getAttr(SessionConstent.PLAYER);
		PlayerProxy playerProxy = player.getPlayerProxy();
		
		RemoteSocialServer remoteSocialServer = RemoteUtil.socialSync();
		ChatRoom chatRoom = remoteSocialServer.enterChatRoom(player.getId());
		
		playerProxy.setChatRoomId(chatRoom.getId());
		S2CChatMsg.RespInitChatInfo.Builder builder = S2CChatMsg.RespInitChatInfo.newBuilder();
		List<ChatInfo> infos = ChatMsgBuilder.me().builderChatInfo(chatRoom.getAllMsg());
		builder.addAllMsgs(infos);	
		
		builder.setRoomId(playerProxy.getChatRoomId());
		
		return ProtoUnit.toByte(builder.build());
	}
}
